package com.zhku.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zhku.yygh.cmn.client.DictFeignClient;
import com.zhku.yygh.common.result.Result;
import com.zhku.yygh.hosp.respository.HospitalRepository;
import com.zhku.yygh.hosp.service.DepartmentService;
import com.zhku.yygh.hosp.service.HospitalService;
import com.zhku.yygh.model.hosp.Hospital;
import com.zhku.yygh.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: yygh-parent
 * @BelongsPackage: com.zhku.yygh.hosp.service.impl
 * @Author: "deweyMDH"
 * @CreateTime: 2023-05-14  11:10
 * @Version: 1.0
 */
@Service
public class HospitalServiceImpl implements HospitalService {

    @Autowired
    public HospitalRepository hospitalRepository;

    @Autowired
    public DictFeignClient dictFeignClient;

    @Autowired
    private DepartmentService departmentService;


    @Override
    public void save(Map<String, Object> paramMap) {
        // 先把map数据，转化成对象hospital
        String mapString = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        // 判断数据是否存在数据
        String hoscode = hospital.getHoscode();
        Hospital hospitalExsit =  hospitalRepository.getHospitalByHoscode(hoscode);
        // 如果不存在， 进行添加
        if (null != hospitalExsit){
            hospital.setStatus(hospitalExsit.getStatus());
            hospital.setCreateTime(hospitalExsit.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
        } else {  // 如果平台数据不存在，则插入到后台数据库
            //0：未上线 1：已上线
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }

    // 查询医院接口,
    @Override
    public Hospital getByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

//    获取分页列表
    @Override
    public Page<Hospital> selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        // 1 先创建一个可分页对象 //0为第一页
        Pageable pageable = PageRequest.of(page - 1, limit);
        // 2 创建模糊查询的匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        // 3 把vo转换成实体
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);
        // 4 根据实体和匹配器 做 分页的row 模板
        Example<Hospital> hospitalExample = Example.of(hospital, matcher);
        // 5 根据分页和 row模板，向mongodb查数据
        Page<Hospital> pages = hospitalRepository.findAll(hospitalExample, pageable);

        // 6 做医院等级封装
        pages.getContent().stream().forEach(item -> {
            this.setHospitalHosType(item);
        });

        return pages;
    }

    // mogodb的数据是和mysql里面的数据是相互补充的，需要去数据字典服务获得一些额外的数据
    private Hospital setHospitalHosType(Hospital hospital) {
        // 根据dictCode和value 获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        // 查询省 + 市 + 区
        String provinceString = dictFeignClient.getName(hospital.getProvinceCode());
        String cityString = dictFeignClient.getName(hospital.getCityCode());
        String districtString = dictFeignClient.getName(hospital.getDistrictCode());

        hospital.getParam().put("fullAddress", provinceString + cityString + districtString);
        hospital.getParam().put("hostypeString", hostypeString);

        return hospital;
    }

    //    更新医院的上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue() == 0 || status.intValue() == 1) {
            // 现在mongodb查到那个数据
            Hospital hospital = hospitalRepository.findById(id).get();
            // 设置 修改的值
            hospital.setStatus(status);
            hospital.setUpdateTime(new Date());
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> getHospById(String id) {
        Map<String, Object> result = new HashMap<>();

        Hospital hospital = this.setHospitalHosType(hospitalRepository.findById(id).get());
        // 设置医院基本信息
        result.put("hospital", hospital);
        //单独处理更直观
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

    //获取医院名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        if (null != hospital){
            return hospital.getHosname();
        }
        return "";
    }

    // 根据医院名称获取医院列表
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    // 医院预约挂号详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }

}
